package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.gamelib.Utils;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;

public class Replicator extends Component
{
	private ImageAnimation platform;
	private Image selector;
	private ImageStrip turbine;
	private boolean replicating = false;
	
	public Replicator(Point pos)
	{
		super(pos.x,pos.y);
		if(platform == null)
		{
			platform = new ImageAnimation(new ImageStrip(ResourceManager.getImage("replicator.png"), 40, 20, 16), 4);
			selector = ResourceManager.getImage("selector.png");
			turbine = new ImageStrip(ResourceManager.getImage("selector_icons.png"), 15,25);
		}
	}
	
	public Point getSize()
	{
		return new Point(40,20+selector.getHeight(null));
	}
	
	public boolean canMove()
	{
		return false;
	}
	
	public void onClick(Point point)
	{
		// is the user clicking on the selector?
		if(point.y < selector.getHeight(null))
		{
			if(Math.abs(point.x - getSize().x/2) < selector.getWidth(null))
			{
				
			}
		}
	}

	public void paint(Graphics g, int px, int py)
	{
		g.drawImage(selector, px+(getSize().x-selector.getWidth(null))/2, py, null);
		int cx = getSize().x/2;
//		int cy = selector.getHeight(null)/2;
		turbine.paint(g, 0, px + cx -1, py + selector.getHeight(null) - 5,Utils.ANCHOR_HCENTER|Utils.ANCHOR_BOTTOM);
		platform.paintCurrentFrame(g, px, py+selector.getHeight(null));
	}
	
	public void onCollided(Component other)
	{
		if(replicating)
			return;
		if(other instanceof Metal)
		{
			Game.game.components.remove(other);
			// replicate
			platform.play();
			replicating = true;
			Sound.play("replicate.wav");
		}
	}
	
	public boolean isMadeOfMetal()
	{
		return true;
	}
	
	public void update()
	{
		platform.update();
		if(platform.isOver() && replicating)
		{
			Game.game.add(new Turbine(new Point(x+21,y + 12 )));
			replicating = false;
			platform.rewind();
			platform.stop();
		}
	}
}
